package com.zl.credit.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.zl.credit.entity.CreditProgress;
import com.zl.credit.entity.CreditUserMessage;
import com.zl.credit.service.CreditProgressService;
import com.zl.credit.service.impl.CreditUserMessageServiceImpl;
import com.zl.credit.util.MySendSms;
import com.zl.credit.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author anonymous
 * @since 2020-07-14
 */
@Controller
@RequestMapping("/credit-user-message")
public class CreditUserMessageController {

    @Autowired
    private CreditUserMessageServiceImpl userMessageService;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private MySendSms sendSms;

    @Autowired
    private CreditProgressService progressService;

    @RequestMapping(value = "CheckUserMessage",method = RequestMethod.POST)
    public String CheckUserMessage(CreditUserMessage userMessage, String yzmcode, String name, Model model){
        System.out.println("userMessage = " + userMessage + ", yzmcode = " + yzmcode);
        System.out.println("验证基本信息表");
        //获取redis保存的验证码进行验证
        String yzm = redisTemplate.opsForValue().get("phone:" + userMessage.getPhone()).toString();
        System.out.println("redis取出来的验证码为："+yzm);
        if(yzmcode.equals(yzm)){
            //主键生成
            String substring = UUID.randomUUID().toString().substring(0, 6);
            userMessage.setCumId(substring);
            //把基本信息表的信息插入到数据库表中
            userMessageService.addusermessage(userMessage);
            //1.查询当前用户的申请进度表
            CreditProgress progress = progressService.selectprogressstatus(name);
            //更改申请表的基本表id和idcard
            progress.setCumId(substring);
            progress.setIdCard(userMessage.getIdCard());
            progressService.updateprogress(progress);
            model.addAttribute("name",name);
            return "apply2";
        }else{
            return "apply1";
        }
    }

    /**
     * 页面上通过ajax进行实名认证
     * @param cumName
     * @param idCard
     * @return
     */
    @RequestMapping(value = "CheckRealName",method = RequestMethod.POST)
    @ResponseBody
    public String CheckRealName(String cumName,String idCard){
        System.out.println("cumName = " + cumName + ", idCard = " + idCard);

        String desc = userMessageService.CheckRealName(cumName, idCard);
        //返回给前端的结果
        String msg = new String();
        //判断desc的值，
        if("一致".equals(desc)){
            //身份证与姓名一致
            msg="身份证与姓名一致";
        }else{
            msg="身份证与姓名不一致";
        }
        System.out.println("msg:"+msg);
        return msg;
    }

    /**
     * 验证手机号码，调用短信服务给用户发短信验证码
     * @param phone
     * @return
     */
    @RequestMapping(value = "CheckCode",method = RequestMethod.POST)
    public @ResponseBody    String CheckCode(String phone){
        System.out.println("phone = " + phone);
        //返回给前端的信息
        //生成随机验证码
        String code = ((int)(Math.random()*9999) + 100)+"";
        String res = sendSms.sendsms(phone, "SMS_195226739", code);
        System.out.println("code:"+code);
        JSONObject jsonObject = JSONObject.parseObject(res);
        String message = jsonObject.getString("Message");
        System.out.println("message:"+message);

        redisTemplate.opsForValue().set("phone:"+phone,code,5, TimeUnit.MINUTES);

        if("OK".equals(message)){
            System.out.println("短信发送成功，验证码为："+code);
            //edisTemplate.opsForValue().set("phone:"+phone,code,5, TimeUnit.MINUTES);
            return "短信发送成功";
        }else {
            System.out.println("短信发送失败");
            return "短信发送失败,请稍后重试！";
        }
    }
    @RequestMapping(value ="selectAll",method = RequestMethod.GET)
    @ResponseBody
    public Result<CreditUserMessage> selectAll(){

        List<CreditUserMessage> list = userMessageService.list();

        Result<CreditUserMessage> result = new Result<>();
        result.setCode(0);
        result.setMsg("22222");
        result.setData(list);
        result.setCount();
        /*String string = JSON.toJSONString(list);
        System.out.println(string);
        JSONArray array = JSONArray.parseArray(string);
        System.out.println("array:"+array);
        result.put("data", array);*/
        // 将其转换为JSON数据，并压入值栈返回
        /*String json = JSON.toJSONString(result);
        JSONObject object = JSON.parseObject(json);
        System.out.println("object"+object);*/
        return result;
    }

    @GetMapping("haha")
    public String info(){
        System.out.println("ahha");
        return "info2";
    }

}
